//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.planw.beetl.sql.marker;

import com.intellij.codeInsight.daemon.RelatedItemLineMarkerInfo;
import com.intellij.codeInsight.daemon.RelatedItemLineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.markup.GutterIconRenderer.Alignment;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.planw.beetl.service.BeetlMapperService;
import com.planw.beetl.service.BeetlSqlService;
import com.planw.beetl.utils.ConstUtil;
import com.planw.beetl.utils.ContextIcons;
import com.planw.beetl.utils.PsiKt;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;

public class BeetlSqlMapperLineMarkerProvider extends RelatedItemLineMarkerProvider {
    private static final Logger logger = Logger.getInstance(BeetlSqlMapperLineMarkerProvider.class);

    public BeetlSqlMapperLineMarkerProvider() {
    }

    @Override
    protected void collectNavigationMarkers(@NotNull PsiElement element, @NotNull Collection<? super RelatedItemLineMarkerInfo<?>> result) {
        if (element == null) {
            return;
        }

        if (result == null) {
            return;
        }

        logger.info("调用Mapper增加跳转");
        if (!ConstUtil.isReady()) {
            logger.info("项目未准备完成");
        } else if (element instanceof PsiClass) {
            Project project = element.getProject();
            BeetlSqlService beetlSqlService = BeetlSqlService.getInstance(project);
            BeetlMapperService beetlMapperService = BeetlMapperService.getInstance(project);
            PsiClass psiClass = (PsiClass) element;
            if (!beetlMapperService.isBeetlMapper(psiClass)) {
                logger.info("当前类文件不属于接口或未继承beetl BaseMapper接口");
            } else {
                PsiFile sqlFile = beetlMapperService.findSqlFile(psiClass);
                if (sqlFile == null) {
                    logger.info("无法找到对应的markdown文件");
                } else {
                    Map<PsiMethod, PsiElement> methodAndSqlIdMap = beetlMapperService.getMethodAndSqlIdMap(sqlFile, psiClass, (PsiMethod) null);
                    if (methodAndSqlIdMap == null) {
                        logger.info("无法获取SqlId与接口方法的对应Map");
                    } else {
                        String sqlFileName = sqlFile.getName();

                        for (Map.Entry<PsiMethod, PsiElement> entry : methodAndSqlIdMap.entrySet()) {
                            PsiElement psiElement = (PsiElement) entry.getValue();
                            NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(ContextIcons.MAPPER_LINE_MARKER_ICON).setAlignment(Alignment.CENTER).setTargets(Collections.singleton(psiElement)).setTooltipTitle("Navigation to " + psiElement.getText() + " in " + sqlFileName);
                            PsiIdentifier psiIdentifier = (PsiIdentifier) PsiKt.findChildByClass((PsiElement) entry.getKey(), PsiIdentifier.class);
                            logger.info("创建md的" + psiElement + "内容与方法" + psiIdentifier + "名称的跳转图标");
                            if (psiIdentifier != null) {
                                result.add(builder.createLineMarkerInfo(psiIdentifier));
                            }
                        }

                    }
                }
            }
        }
    }
}
